package com.zmn.oms.zmn.facade.impl.open.v2;

import com.alibaba.fastjson.JSON;
import com.zmn.oms.common.annotation.EntrustOrderLog;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.entrust.SyncEntrustOrderStatusDTO;
import com.zmn.oms.model.dto.open.v2.PushDutyTimeDTO;
import com.zmn.oms.model.dto.open.v2.PushMasterInfoDTO;
import com.zmn.oms.model.dto.open.v2.PushOrderCompleteDTO;
import com.zmn.oms.model.dto.open.v2.PushOrderDTO;
import com.zmn.oms.zmn.business.interfaces.entrust.EntrustOrderNotifyBService;
import com.zmn.oms.zmn.facade.interfaces.open.v2.V2OrderStatusAcceptFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/01/06 19:20
 */
@Slf4j
@Service("v2OrderStatusAcceptFacade")
public class V2OrderStatusAcceptFacadeImpl implements V2OrderStatusAcceptFacade {

    @Autowired
    private EntrustOrderNotifyBService entrustOrderNotifyBService;

    @Override
    @EntrustOrderLog(type = OrderStatusConsts.ORDER_STATUS_CONFIRM)
    public ResultDTO acceptDutyTime(PushDutyTimeDTO pushDutyTimeDTO) throws OmsBaseException {
        log.info("#V2OrderStatusAcceptFacade#acceptDutyTime#：PushDutyTimeDTO=[{}]", JSON.toJSONString(pushDutyTimeDTO));

        // 校验数据完整性
        validatePushOrderDTO(pushDutyTimeDTO);

        // 执行业务处理
        SyncEntrustOrderStatusDTO dto = entrustOrderNotifyBService.prepareSyncDutyTime(pushDutyTimeDTO);
        entrustOrderNotifyBService.doSyncDutyTime(dto);

        return ResultDTO.success(dto);
    }

    @Override
    @EntrustOrderLog(type = OrderStatusConsts.ORDER_STATUS_DISTRIBUTE)
    public ResultDTO<SyncEntrustOrderStatusDTO> acceptMasterInfo(PushMasterInfoDTO pushMasterInfoDTO)
            throws OmsBaseException {
        log.info("#V2OrderStatusAcceptFacade#acceptMasterInfo#：PushMasterInfoDTO=[{}]",
                JSON.toJSONString(pushMasterInfoDTO));

        // 校验数据完整性
        validatePushOrderDTO(pushMasterInfoDTO);

        // 执行业务处理
        SyncEntrustOrderStatusDTO dto = entrustOrderNotifyBService.prepareSyncMasterInfo(pushMasterInfoDTO);
        entrustOrderNotifyBService.doSyncMasterInfo(dto);

        return ResultDTO.success(dto);
    }

    @Override
    @EntrustOrderLog(type = OrderStatusConsts.ORDER_STATUS_VISIT)
    public ResultDTO<SyncEntrustOrderStatusDTO> acceptMasterVisit(PushOrderDTO pushOrderDTO) throws OmsBaseException {
        log.info("#V2OrderStatusAcceptFacade#acceptMasterVisit#：PushOrderDTO=[{}]", JSON.toJSONString(pushOrderDTO));

        // 校验数据完整性
        validatePushOrderDTO(pushOrderDTO);

        // 执行业务处理
        SyncEntrustOrderStatusDTO dto = entrustOrderNotifyBService.prepareSyncMasterVisit(pushOrderDTO);
        entrustOrderNotifyBService.doSyncMasterVisit(dto);

        return ResultDTO.success(dto);
    }

    @Override
    @EntrustOrderLog(type = OrderStatusConsts.ORDER_STATUS_COMPLETE)
    public ResultDTO<SyncEntrustOrderStatusDTO> acceptOrderComplete(PushOrderCompleteDTO pushOrderCompleteDTO)
            throws OmsBaseException {
        log.info("#V2OrderStatusAcceptFacade#acceptOrderComplete#：PushOrderDTO=[{}]", JSON.toJSONString(pushOrderCompleteDTO));

        // 校验数据完整性
        validatePushOrderDTO(pushOrderCompleteDTO);

        // 执行业务处理
        SyncEntrustOrderStatusDTO dto = entrustOrderNotifyBService.prepareSyncOrderComplete(pushOrderCompleteDTO);
        entrustOrderNotifyBService.doSyncOrderComplete(dto);

        return ResultDTO.success(dto);
    }

    @Override
    @EntrustOrderLog(type = OrderStatusConsts.ORDER_STATUS_CANCEL)
    public ResultDTO<SyncEntrustOrderStatusDTO> acceptOrderCancel(PushOrderDTO pushOrderDTO) throws OmsBaseException {
        log.info("#V2OrderStatusAcceptFacade#acceptOrderCancel#：PushOrderDTO=[{}]", JSON.toJSONString(pushOrderDTO));

        // 校验数据完整性
        validatePushOrderDTO(pushOrderDTO);

        // 执行业务处理
        SyncEntrustOrderStatusDTO dto = entrustOrderNotifyBService.prepareSyncOrderCancel(pushOrderDTO);
        entrustOrderNotifyBService.doSyncOrderCancel(dto);

        return ResultDTO.success(dto);
    }

    /*
     * 校验业务参数完整性
     */
    private void validatePushOrderDTO(PushOrderDTO pushOrderDTO) throws OmsBaseException {
        // 基本参数
        if (pushOrderDTO == null || pushOrderDTO.getThirdOrderId() == null || pushOrderDTO.getOptTime() == null) {
            throw new OmsBaseException("订单号和操作时间不能为空");
        }

        // 同步预约时间
        if (pushOrderDTO instanceof PushDutyTimeDTO) {
            PushDutyTimeDTO pushDutyTimeDTO = (PushDutyTimeDTO) pushOrderDTO;
            if (pushDutyTimeDTO.getDutyTime() == null) {
                throw new OmsBaseException("预约时间不能为空");
            }
        }
        // 同步派单工程师
        else if (pushOrderDTO instanceof PushMasterInfoDTO) {
            PushMasterInfoDTO pushMasterInfoDTO = (PushMasterInfoDTO) pushOrderDTO;
            if (pushMasterInfoDTO.getMasterName() == null || pushMasterInfoDTO.getMasterPhone() == null) {
                throw new OmsBaseException("工程师名称和手机号不能为空");
            }
        }
    }
}
